import Taro, {Component} from '@tarojs/taro'
import {View} from "@tarojs/components"
import {AtTabBar, AtTabs, AtTabsPane} from 'taro-ui'
import Notice from '../classNotice/index'
import Homework from '../classHomework/index'
import Files from '../classFiles/index'
import Chatroom from '../classChatroom/index'
import {login,getUser} from "./service";
import {getGlobalData,setGlobalData} from "../../global.d";
import {getUserById} from '../../service/index'
import {navigateWithAuth} from "../../utils";
import './index.scss'

export default class Index extends Component {
    constructor(props) {
        super(props);
        this.handleClick = this.handleClick.bind(this);
        this.state = {
            current: 0
        }
    }
  componentWillMount() {
    let openId = getGlobalData('openId');
    if (!openId) {
      login();
    }else {
      this.getUserDirectly()
    }

  }
  componentDidMount () {
    Taro.setNavigationBarTitle({
      title:'班级'
    })
  }
   async getUserDirectly() {
     let user = await getUser();
     setGlobalData('user', user)
   }

  handleClick(value){
    this.setState({
      current:value
    })
  }

  render() {
    const tabList = [
      {title:'班级通知',iconPrefixClass:'icon',iconType:'tongzhi'},
      {title:'班级作业',iconPrefixClass:'icon',iconType:'zuoye'},
      {title:'班级文件',iconPrefixClass:'icon',iconType:'wenjian'}

    ]
    const {current} = this.state;
      return(
        <View   >
          <View className='no_top_line'>
            <AtTabBar
              tabList={tabList}
              onClick={this.handleClick}
              current={current}
              iconSize={24}
              className='no_top_line'
            />
          </View>

          <AtTabs current={current} tabList={tabList} swipeable onClick={this.handleClick}>
            <AtTabsPane current={current} index={0}>
              {current === 0 && <Notice />}
            </AtTabsPane>
            <AtTabsPane current={current} index={1}>
              {current === 1 && <Homework />}
            </AtTabsPane>
            <AtTabsPane current={current} index={2}>
              {current === 2 && <Files />}
            </AtTabsPane>
          </AtTabs>
        </View>
      )
  }
}
